<?php

namespace DesignModel\decorator;
/**
 * Class Canvas
 * @description: 画一个正方形
 * @time:2018年6月20日17:42:33
 * @Author: yfl
 * @QQ 554665488
 */
class Canvas2
{
    public $data;

    protected $decorators = array(); //用来放装饰器的盒子

    public function init($width = 20, $height = 10)
    {
        $data = array();
        for ($i = 0; $i < $height; $i++) {
            for ($j = 0; $j < $width; $j++) {
                $data[$i][$j] = '*';
            }
        }
        $this->data = $data;
    }

    /**
     * @description: 用来改变画图之前的动作
     * @var void
     * Date: 2018-6-20 21:47
     */
    public function beforeDraw()
    {
        var_dump($this->decorators,'之前');
        foreach ($this->decorators as $decorator) {  //这里是按照先进先出原则
//            echo 'before';
            $decorator->beforeDraw();
        }
    }

    /**
     * @description:用来改变画图之后的动作
     * @var void
     * Date: 2018-6-20 21:50
     */
    public function afterDraw()
    {
        var_dump($this->decorators,'之后');
        $decorators = array_reverse($this->decorators); //这里要按照使用  控制反转！！！！！ 按照先进后出 后进先出的原则 （和栈的原则一样）
        var_dump($decorators,'之后');
        foreach ($decorators as $decorator) {  //
//            echo 'after';
            $decorator->afterDraw();
        }
    }

    /**
     * @description:设置改变动作的装饰器
     * @var void
     * Date: 2018-6-20 21:52
     * @param DecoratorApi $decoratorApi
     */
    public function setDecorators(DecoratorApi $decoratorApi)
    {
        $this->decorators[] = $decoratorApi;
    }

    public function draw()
    {
        $this->beforeDraw();
        foreach ($this->data as $index => $line) {
            foreach ($line as $item) {
//                echo $item;
            }
//            echo "<br />";
//            echo "\n";
        }
        $this->afterDraw();
    }

    /**
     * @description:
     * @var void
     * Date: 2018-6-20 19:53
     * @param $a1 : 开始行
     * @param $a2 :结束行
     * @param $b1 :开始列
     * @param $b2 :结束列
     */
    public function rect($a1, $a2, $b1, $b2)
    {
        foreach ($this->data as $index1 => $line) {

            if ($index1 < $a1 or $index1 > $a2) continue;
//            echo $index1."\n";
            foreach ($line as $index2 => $item) {
                if ($index2 < $b1 or $index2 > $b2) continue;
                $this->data[$index1][$index2] = '&nbsp;';
            }
        }
    }
}

//$obj = new Canvas();
//$obj->init();
//$obj->rect(3, 6, 5, 14);
//$obj->draw();